package com.linkcard.list.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.tools.Helper;
import com.linkcard.commen.tools.Page;
import com.linkcard.list.manager.ActivitiesManager;
import com.linkcard.list.manager.CategoryManager;
import com.linkcard.list.model.Activities;
import com.linkcard.list.model.Business;
import com.linkcard.list.model.Category;
import com.linkcard.user.model.WebsiteColumns;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ActivitiesAction extends ActionSupport {

	private ActivitiesManager activitiesManager=null;
	private CategoryManager categoryManager=null;
	private Activities activities=null;
	private Page pageObj=null;
	private int page;
	private int rows;
	private String searchStr=null;
	private int columnId;
	private int ids;
	private List list=null;
	private List categoryList=null;
	private String path=null;
	@Override
	public String execute() throws Exception {
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(20);
		list=this.activitiesManager.activitesList(pageObj,(searchStr==null)?"":Helper.decode(searchStr));
		categoryList=this.categoryManager.categoryListBySelect(7,0);
		WebsiteColumns column=null;
		List columnList=(List)ActionContext.getContext().getApplication().get("columns");
		for(int i=0;i<columnList.size();i++)
		{
			 column=(WebsiteColumns)columnList.get(i);
			if(column.getId()==7)
			{
				path="<a href='"+column.getUrl()+".action?columnId="+column.getId()+"'>"+column.getName()+"</a>";
				break;
			}
		}
		if(searchStr==null)
		{
		     path+=">>所有信息";
		     path="<span>位置："+path+"</span><h1>最新活动</h1>";
		}else
		{
			String[] cids=searchStr.split("_");
			path+=">><a href='activities.action?page=1&searchStr="+cids[0]+"_-2_-2'>"+categoryManager.findCategory(Integer.parseInt(cids[0])).getName()+"</a>";
		    if(!cids[1].equals("-2"))
		    {
		    	path+=">><a href='activities.action?page=1&searchStr="+searchStr+"'>"+categoryManager.findCategory(Integer.parseInt(cids[1])).getName()+"</a>";
		    }
		    path="<span>位置："+path+"</span><h1>"+categoryManager.findCategory(Integer.parseInt(cids[0])).getName()+"</h1>";
		}
		return "success";
	}
	//添加活动
	public String addActivites() throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		activities.setDatetime(sdf.format(new Date()));
		boolean isSuccess=this.activitiesManager.addActivities(activities);
		if(!isSuccess)
		{
			this.addActionMessage("添加失败!");
			return "Activites";
		}
		return "List";
	}
	//获得活动列表
	public String activitesListForJson() throws Exception{
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(rows);
		searchStr=Helper.decode(searchStr);
		List list=this.activitiesManager.activitesList(pageObj,searchStr);
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			Object[] obj=(Object[])list.get(i);
			Activities activities=(Activities)obj[0];
			Category category=(Category)obj[1];
			Business business=(Business)obj[2];
			
			JSONObject temp=new JSONObject();
			temp.put("id", activities.getId());
			temp.put("title", activities.getTitle());
			temp.put("category", category.getName());
			Category subCategory=categoryManager.findCategory(activities.getSubCategoryId());
			temp.put("subCategory",(subCategory!=null)?subCategory.getName():"");
			temp.put("datetime", activities.getDatetime());
			temp.put("businessId", business.getName());
			temp.put("image", activities.getImage());
			temp.put("description",activities.getDescription());
			temp.put("opt","<a target='_blank' href='activities!activitiesDetial.action?activities.id="+activities.getId()+"'>查看</a>&nbsp;&nbsp;<a href='activities!modify.action?columnId="+columnId+"&activities.id="+activities.getId()+"'>修改</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='deleteActivities("+activities.getId()+");'>删除</a>");
			jsonArray.add(temp);
		}
		json.put("total", pageObj.getRowCount());
		json.put("rows", jsonArray);
		backText(json.toString());
		return "Data";
	}
	//修改活动
	public String modify() throws Exception{
		this.clearMessages();
		activities=this.activitiesManager.findActivities(activities.getId());
		return"Activities";
	}
	public String modifyActivites() throws Exception{
		boolean isSuccess=this.activitiesManager.modifyActivities(activities);
		if(!isSuccess)
		{
			this.addActionMessage("修改失败");
			return"Activities";
		}
		return"List";
	}
	//删除商家
	public String deleteActivities() throws Exception{
		boolean isSuccess=this.activitiesManager.deleteActivities(ids);
		JSONObject json=new JSONObject();
		json.put("info",isSuccess);
		backText(json.toString());
		return "Data";
	}
	//获得单个活动
	public String activitiesDetial() throws Exception{
		categoryList=this.categoryManager.categoryListBySelect(7,0);
		list=this.activitiesManager.singleActivities(activities.getId());
		Activities act=(Activities)((Object[])list.get(0))[0];
		Category category=null;
		
		if(act.getSubCategoryId()!=-2)
		{
			category=this.categoryManager.findCategory(act.getSubCategoryId());
		}else
		{
			category=new Category();
			category.setName("");
		}
		list.add(category);
		
		WebsiteColumns column=null;
		List columnList=(List)ActionContext.getContext().getApplication().get("columns");
		for(int i=0;i<columnList.size();i++)
		{
			 column=(WebsiteColumns)columnList.get(i);
			if(column.getId()==4)
			{
				path="<a href='"+column.getUrl()+".action?columnId="+column.getId()+"'>"+column.getName()+"</a>";
				break;
			}
		}
		 category=categoryManager.findCategory(act.getCategoryId());
		path+=">><a href='activities.action?page=1&searchStr="+category.getId()+"_-2_-2'>"+category.getName()+"</a>";
	    if(act.getSubCategoryId()!=-2)
	    {
	    	Category subCategory=categoryManager.findCategory(act.getSubCategoryId());
	    	path+=">><a href='activities.action?page=1&searchStr="+category.getId()+"_"+subCategory.getId()+"_-2'>"+subCategory.getName()+"</a>";
	    }
		return "ActivitiesDetial";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public ActivitiesManager getActivitiesManager() {
		return activitiesManager;
	}
	public void setActivitiesManager(ActivitiesManager activitiesManager) {
		this.activitiesManager = activitiesManager;
	}
	public Activities getActivities() {
		return activities;
	}
	public void setActivities(Activities activities) {
		this.activities = activities;
	}
	public Page getPageObj() {
		return pageObj;
	}
	public void setPageObj(Page pageObj) {
		this.pageObj = pageObj;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) {
		this.searchStr = searchStr;
	}
	public CategoryManager getCategoryManager() {
		return categoryManager;
	}
	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}
	public int getColumnId() {
		return columnId;
	}
	public void setColumnId(int columnId) {
		this.columnId = columnId;
	}
	public int getIds() {
		return ids;
	}
	public void setIds(int ids) {
		this.ids = ids;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public List getCategoryList() {
		return categoryList;
	}
	public void setCategoryList(List categoryList) {
		this.categoryList = categoryList;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}

}
